% Number of time samples.
Ns = 600;

% Heater supply voltage.
V_h = 12;

% Heater resistance (ohms).
R_h = 15;

% Heater power (watts).
P = V_h.^2 / R_h;

% Ambient temperature (celsius).
T_a = 20;

% Initial temperature (celsius).
T_0 = T_a;

% Setpoint temperature (celsius).
T_s = 40;

% Sampling interval (seconds).
delta_t = 1;

% Temperature history (celsius).
T = zeros (Ns, 1);

% Heater power history (watts).
H = zeros (Ns, 1);

model = toaster_model_init (delta_t, T_0, T_a);

% Initial heater setting.
H(1) = P;


for n = 1 : Ns

  % Get current model temperature.
  T(n) = toaster_model_temp (model);
  
  % Calculate temperature error.
  e = T_s - T(n);

  % Implement bang-bang controller.
  if e > 0
    H(n) = P;
  else
    H(n) = 0;
  end
  
  % Update model.
  model = toaster_model_update (model, H(n), T_a);
end


t = [0 : Ns - 1].' * delta_t;

toaster_plot (t, H, T, T_a, T_s);
